package com.guobox.movies.view.adapter;

import android.animation.ObjectAnimator;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.ScaleAnimation;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.guobox.movies.R;
import com.guobox.movies.proxy.HttpUrls;
import com.guobox.movies.entity.MovieEntity;

public class StagePicsAdapter extends RecyclerView.Adapter<StagePicsAdapter.StagePicHolder> {
    String[] stagePicsIds;
    MovieEntity movieEntity;
    Context context;
    private StageRvCallback stageRvCallback;
    public StagePicsAdapter(Context context, MovieEntity movieEntity,StageRvCallback stageRvCallback) {
        this.context = context;
        this.stagePicsIds = TextUtils.split(movieEntity.getMetadata().getStages(), ",");
        this.movieEntity = movieEntity;
        this.stageRvCallback = stageRvCallback;
    }

    @Override
    public StagePicHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new StagePicHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_stagepics, null));
    }

    @Override
    public void onBindViewHolder(final StagePicHolder holder, int position) {
        Glide.with(context)
                .load(HttpUrls.getMovieStageURL(movieEntity.getMetadata().getMovieId(),stagePicsIds[position], HttpUrls.Pic_Middle).build().toString())
                .override(426, 240)
                .centerCrop()
                .into(holder.stagePicsIV);
        holder.itemView.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean focused) {
                ImageView bgJZ = (ImageView) view.findViewById(R.id.bg_jz);
                if (focused) {
                    ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(bgJZ, "alpha", 1);
                    objectAnimator.setDuration(400);
                    objectAnimator.start();
                    AnimationSet animationSet = new AnimationSet(true);
                    ScaleAnimation scaleAnimation = new ScaleAnimation(1f, 1.13f, 1f, 1.13f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
                    scaleAnimation.setDuration(400);
                    animationSet.addAnimation(scaleAnimation);
                    animationSet.setFillAfter(true);
                    view.startAnimation(animationSet);
                } else {
                    ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(bgJZ, "alpha", 0);
                    objectAnimator.setDuration(400);
                    objectAnimator.start();
                    AnimationSet animationSet = new AnimationSet(true);
                    ScaleAnimation scaleAnimation = new ScaleAnimation(1.13f, 1f, 1.13f, 1f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
                    scaleAnimation.setDuration(400);
                    animationSet.addAnimation(scaleAnimation);
                    animationSet.setFillAfter(true);
                    view.startAnimation(animationSet);
                }
            }
        });
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                stageRvCallback.onItemClick(holder.getAdapterPosition());
            }
        });

    }


    @Override
    public int getItemCount() {
        return stagePicsIds == null ? 0 : stagePicsIds.length;
    }

    class StagePicHolder extends RecyclerView.ViewHolder {
        ImageView stagePicsIV;
        View itemView;

        StagePicHolder(View itemView) {
            super(itemView);
            this.itemView = itemView;
            itemView.setFocusable(true);
            stagePicsIV = (ImageView) itemView.findViewById(R.id.iv_stage_pic);
        }
    }

    public interface StageRvCallback {
        public void onItemClick(int position);
    }

}